package org.benjamin.lending.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.benjamin.lending.domain.entity.MemberInfo;
import org.benjamin.lending.domain.entity.MemberMoneyLog.Type;
import org.benjamin.lending.domain.entity.RechargeLog;
import org.benjamin.lending.domain.entity.TransactType;
import org.benjamin.lending.domain.form.RechargeForm;
import org.benjamin.lending.repository.MemberInfoRepository;
import org.benjamin.lending.repository.RechargeLogRepository;
import org.benjamin.lending.service.MemberInfoService;
import org.benjamin.lending.service.RechargeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

@Service
public class RechargeServiceImpl implements RechargeService {
	
	@Autowired
	private RechargeLogRepository rechargeLogRepository;
	
	@Autowired
	private MemberInfoRepository memberInfoRepository;
	
	@Autowired
	private MemberInfoService memberInfoService;

	@Override
	public Page<RechargeLog> findRechargeLogPage(final RechargeForm rechargeForm, Pageable pageable) {
		Specification<RechargeLog> specification = new Specification<RechargeLog>() {
			
			@Override
			public Predicate toPredicate(Root<RechargeLog> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<Predicate>();
				if(StringUtils.isNotBlank(rechargeForm.getUsername())) {
					list.add(cb.like(root.get("memberInfo").get("username").as(String.class), "%"+rechargeForm.getUsername()+"%"));	
				}
				Predicate[] predicates = new Predicate[list.size()];
				return query.where(list.toArray(predicates)).getGroupRestriction();
			}
		};
		return this.rechargeLogRepository.findAll(specification,pageable);
	}
	
	@Override
	public RechargeLog userRecgargeRequest(RechargeForm form,String username) {
		MemberInfo memberInfo = this.memberInfoRepository.findByUsername(username);
		RechargeLog rechargeLog = new RechargeLog();
		rechargeLog.setAmount(form.getAmount());
		rechargeLog.setBankCode(form.getBankID());
		rechargeLog.setMemberInfo(memberInfo);
		rechargeLog.setStatus(false);
		rechargeLog.setCreateTime(new Timestamp(System.currentTimeMillis()));
		this.rechargeLogRepository.save(rechargeLog);
		return rechargeLog;
		
	}

	@Override
	public void finshSuccess(Integer rechargeId) {
		RechargeLog rechargeLog = this.rechargeLogRepository.findOne(rechargeId);
		rechargeLog.setFinishTime(new Timestamp(System.currentTimeMillis()));
		rechargeLog.setStatus(true);
		this.rechargeLogRepository.save(rechargeLog);
		this.memberInfoService.recordMoneyChange(Type.USER_RECHARGE, rechargeLog.getAmount(), rechargeLog.getMemberInfo(), TransactType.SUMAPAY, rechargeLog);
	}

	@Override
	public void finshFaill(Integer rechargeId) {
		// TODO Auto-generated method stub
		
	}

}
